package com.kim.rssreader.adapter;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kim.rssreader.R;
import com.kim.rssreader.domain.RssColumn;
import com.kim.rssreader.utils.ValueConstant;

public class ColumnGridAdapter extends BaseAdapter {

	List<RssColumn> columns;
	Context context;
	LayoutInflater layoutInflater;

	int width = 0;

	boolean isLastPage = false;

	private static final int[] backs = new int[] { R.drawable.rss_item_back_1,
			R.drawable.rss_item_back_2, R.drawable.rss_item_back_3,
			/* R.drawable.rss_item_back_4, */R.drawable.rss_item_back_5,
			R.drawable.rss_item_back_6, R.drawable.rss_item_back_7,
			R.drawable.rss_item_back_8 };

	private Random backRandom = new Random();

	public ColumnGridAdapter(Context context, List<RssColumn> columns) {
		this.columns = columns;
		if (this.columns == null) {
			throw new IllegalArgumentException("columns can not be null");
		}
		this.context = context;
		this.layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		width = context.getResources().getDisplayMetrics().widthPixels;
		width = (width - ValueConstant.GRID_HORIZONTALSPACING * 4) / 2;
	}

	public ColumnGridAdapter(Context context, List<RssColumn> columns,
			boolean isLastPage) {
		this.columns = columns;
		if (this.columns == null) {
			throw new IllegalArgumentException("columns can not be null");
		}
		this.context = context;
		this.layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		width = context.getResources().getDisplayMetrics().widthPixels;
		width = (width - ValueConstant.GRID_HORIZONTALSPACING * 4) / 2;
		this.isLastPage = isLastPage;
	}

	public void resetData(List<RssColumn> columns) {

		if (this.columns == null) {
			this.columns.clear();
			this.columns = null;
		}

		this.columns = columns;
		if (this.columns == null) {
			throw new IllegalArgumentException("columns can not be null");
		}
	}

	@Override
	public int getCount() {
		if (isLastPage) {
			return columns.size() + 1;
		}
		return columns.size();
	}

	@Override
	public RssColumn getItem(int position) {
		if (position >= columns.size()) {
			return null;
		}
		return columns.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.rss_column_grid_item,
					null);
			viewHolder = new ViewHolder();
			viewHolder.relativeLayout = (RelativeLayout) convertView
					.findViewById(R.id.fl_rss_grid_item_container);
			LayoutParams layoutParams = (LayoutParams) viewHolder.relativeLayout
					.getLayoutParams();
			if (layoutParams == null) {
				layoutParams = new LayoutParams(width, width);
			} else {
				layoutParams.width = width;
				layoutParams.height = width;
			}
			viewHolder.relativeLayout.setLayoutParams(layoutParams);
			viewHolder.textView = (TextView) convertView
					.findViewById(R.id.txt_rss_grid_item_name);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// 随机设置rss项背景
		int backIndex = backRandom.nextInt(backs.length);
		viewHolder.relativeLayout.setBackgroundResource(backs[backIndex]);
		viewHolder.relativeLayout.getBackground().setAlpha(ValueConstant.RSSCOLUMN_ITEM_ALPHA);

		if (position >= this.columns.size()) {
			viewHolder.textView.setText("");
			viewHolder.textView.setBackgroundResource(R.drawable.addmore1);
			return convertView;
		}

		viewHolder.textView.setText(getItem(position).rssColumnName);

		return convertView;
	}

	/*
	 * private class ViewHolder { TextView textView; LinearLayout frameLayout; }
	 */

	private class ViewHolder {
		TextView textView;
		RelativeLayout relativeLayout;
	}

}
